package com.student.status.action;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.student.status.model.UserInfoEntity;

import com.student.status.service.UserService;
import com.student.status.service.serviceimpl.UserServiceImpl;
import com.student.status.util.DateUtil;
import com.student.status.util.ReturnUtil;
import com.student.status.util.StrResponse;
import com.student.status.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@Scope("prototype")
public class UserAction extends ActionSupport {

    private ModelMap mymp;
    @Autowired
    private UserService userService;

    public ModelMap getModelMap() {
        return mymp;
    }
    public void setModelMap(ModelMap mymp) {
        this.mymp = mymp;
    }


    public String userLogin(){

        UserInfoEntity user = JSON.parseObject(StrResponse.getStrResponse().trim().toString(), UserInfoEntity.class);
        if(StringUtils.isEmpty(user.getUserLoginName())){
            mymp = ReturnUtil.error("用户名不能为空");
            return ERROR;
        }
        if(StringUtils.isEmpty(user.getUserLoginPwd())){
            mymp = ReturnUtil.error("密码不能为空");
            return ERROR;
        }

        UserInfoEntity userBean = userService.selectName(user);
        if (userBean == null){
            mymp = ReturnUtil.error("用户名错误",userBean);
            return ERROR;
        }

        UserInfoEntity userBean1 = userService.selectPwd(user);
        if (userBean1 == null){
            mymp = ReturnUtil.error("密码错误",userBean1);
            return ERROR;
        }

        mymp = ReturnUtil.success("登录成功",userBean1);
        return SUCCESS;
    }

    public String userInfo(){
        UserInfoEntity user = JSON.parseObject(StrResponse.getStrResponse(), UserInfoEntity.class);

        if(StringUtils.isEmpty(user.getId())){
            mymp = ReturnUtil.error("用户ID不能为空");
            return ERROR;
        }

        UserInfoEntity userBean = userService.selectById(user);

        if (userBean == null){
            mymp = ReturnUtil.error("查询用户信息失败",userBean);
            return ERROR;
        }
        mymp = ReturnUtil.success("查询用户信息成功",userBean);

        return SUCCESS;
    }


    public String allUserList(){
        List<UserInfoEntity> userBean = userService.selectAll();
        if (userBean == null){
            mymp = ReturnUtil.error("查询所有用户获取失败",userBean);
            return ERROR;
        }
        mymp = ReturnUtil.success("查询所有用户成功",userBean);

        return SUCCESS;
    }

    //
    public String deleteUser(){
        UserInfoEntity user = JSON.parseObject(StrResponse.getStrResponse(), UserInfoEntity.class);
        if(StringUtils.isEmpty(user.getId())){
            mymp = ReturnUtil.error("用户ID不能为空");
            return ERROR;
        }

        int count = userService.deleteUser(user);

        if (count == 0){
            mymp = ReturnUtil.error("删除用户失败");
            return ERROR;
        }
        mymp = ReturnUtil.success("删除用户成功");
        return SUCCESS;
    }

    public String insertUser(){
        UserInfoEntity user = JSON.parseObject(StrResponse.getStrResponse(), UserInfoEntity.class);

        if(StringUtils.isEmpty(user.getUserLoginName())){
            mymp = ReturnUtil.error("用户名不能为空");
            return ERROR;
        }
        if(StringUtils.isEmpty(user.getUserLoginPwd())){
            mymp = ReturnUtil.error("密码不能为空");
            return ERROR;
        }

        UserInfoEntity userBean = userService.selectName(user);
        if (userBean != null){
            mymp = ReturnUtil.error("用户已存在");
            return ERROR;
        }

        String id = UuidUtil.getUUID();
        user.setId(id);
        user.setPlaintPwd(user.getUserLoginPwd());

        String password = StrResponse.MD5(user.getUserLoginPwd());
        user.setUserLoginPwd(password);
        user.setCreateTime(DateUtil.getCurrentTime());
        user.setUserLoginName(user.getUserLoginName());
        user.setCreateUserId(UuidUtil.runVerifyCode(10));

        int count = userService.saveUser(user);

        if (count == 0){
            mymp = ReturnUtil.error("用户注册失败");
            return ERROR;
        }
        mymp = ReturnUtil.success("用户注册成功");
        return SUCCESS;
    }

    public String updateUser(){
        UserInfoEntity user = JSON.parseObject(StrResponse.getStrResponse(), UserInfoEntity.class);
        if(StringUtils.isEmpty(user.getId())){
            mymp = ReturnUtil.error("用户ID不能为空");
            return ERROR;
        }

        if (user.getUserName() != null){
            user.setUserName(user.getUserName());
        }

        if (user.getUserTel() != null){
            user.setUserTel(user.getUserTel());
        }

        if (user.getUserSex() != null){
            user.setUserSex(user.getUserSex());
        }

        if (user.getUserHeadUrl() != null){
            user.setUserHeadUrl(user.getUserHeadUrl());
        }

        if (user.getUserAddress() != null){
            user.setUserAddress(user.getUserAddress());
        }

        if (user.getUserBrithday() != null){
            user.setUserBrithday(user.getUserBrithday());
        }

        if (user.getUserPhone() != null){
            user.setUserPhone(user.getUserPhone());
        }

        if (user.getUserEmail() != null){
            user.setUserEmail(user.getUserEmail());
        }

        int count = userService.updateUser(user);

        if (count == 0){
            mymp = ReturnUtil.error("修改用户失败");
            return ERROR;
        }
        mymp = ReturnUtil.success("修改用户成功");
        return SUCCESS;
    }

    public String adminUpdateUserInfo(){
        UserInfoEntity user = JSON.parseObject(StrResponse.getStrResponse(), UserInfoEntity.class);

        if(StringUtils.isEmpty(user.getId())){
            mymp = ReturnUtil.error("用户ID不能为空");
            return ERROR;
        }

        if (user.getUserName() != null){
            user.setUserName(user.getUserName());
        }

        if (user.getUserTel() != null){
            user.setUserTel(user.getUserTel());
        }

        if (user.getUserSex() != null){
            user.setUserSex(user.getUserSex());
        }

        if (user.getUserHeadUrl() != null){
            user.setUserHeadUrl(user.getUserHeadUrl());
        }

        if (user.getUserAddress() != null){
            user.setUserAddress(user.getUserAddress());
        }

        if (user.getUserBrithday() != null){
            user.setUserBrithday(user.getUserBrithday());
        }

        if (user.getUserPhone() != null){
            user.setUserPhone(user.getUserPhone());
        }

        if (user.getUserEmail() != null){
            user.setUserEmail(user.getUserEmail());
        }

        user.setPlaintPwd(user.getUserLoginPwd());
        String password = StrResponse.MD5(user.getUserLoginPwd());
        user.setUserLoginPwd(password);

        int count = userService.adminUpdateUserInfo(user);

        if (count == 0){
            mymp = ReturnUtil.error("修改用户失败");
            return ERROR;
        }
        mymp = ReturnUtil.success("修改用户成功");
        return SUCCESS;
    }

    public String updateUserPwd(){
        UserInfoEntity user = JSON.parseObject(StrResponse.getStrResponse(), UserInfoEntity.class);
        
        if(StringUtils.isEmpty(user.getId())){
            mymp = ReturnUtil.error("用户ID不能为空");
            return ERROR;
        }

        if (user.getUserLoginPwd() == null){
            mymp = ReturnUtil.error("用户新密码不能为空");
            return ERROR;
        }

        user.setPlaintPwd(user.getUserLoginPwd());
        String password = StrResponse.MD5(user.getUserLoginPwd());
        user.setUserLoginPwd(password);

        int count = userService.updateUserPwd(user);

        if (count == 0){
            mymp = ReturnUtil.error("修改用户失败");
            return ERROR;
        }
        mymp = ReturnUtil.success("修改用户成功");
        return SUCCESS;
    }

    public String updateUserLev(){
        UserInfoEntity user = JSON.parseObject(StrResponse.getStrResponse(), UserInfoEntity.class);
        if(StringUtils.isEmpty(user.getId())){
            mymp = ReturnUtil.error("用户ID不能为空");
            return ERROR;
        }

        if (user.getUserLevel() == null){
            mymp = ReturnUtil.error("用户标识不能为空");
            return ERROR;
        }

        user.setUserLevel(user.getUserLevel());

        int count = userService.updateUserLev(user);

        if (count == 0){
            mymp = ReturnUtil.error("修改用户失败");
            return ERROR;
        }

        mymp = ReturnUtil.success("修改用户成功");
        return SUCCESS;
    }

}
